package com.atguigu.eduorder.service.impl;

import com.atguigu.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.eduorder.client.EduClient;
import com.atguigu.eduorder.client.UcenterClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.mapper.OrderMapper;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.eduorder.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-08
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    //生成订单的方法
    @Override
    public String createOrders(String courseId, String memberId) {
        System.out.println("方法实现OrderServiceImpl:");

        //1.通过远程调用根据课程id获取课程信息
        CourseWebVoOrder coursInfoOrder = eduClient.courseWebVoOrder(courseId);
        System.out.println("传入课程id->courseId:" + courseId + "返回课程信息coursInfoOrder:" + coursInfoOrder);

       //2.通过远程调用根据用户id获取用户信息
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);
        System.out.println("传入用户id->courseId:" + memberId + "返回用户信息->userInfoOrder:" + userInfoOrder);

        //创建一个实体类对象，往里面放值
        Order order = new Order();
        order.setCourseId(courseId);//课程id
        order.setOrderNo(OrderNoUtil.getOrderNo());//订单号
        order.setCourseTitle(coursInfoOrder.getTitle());//课程名称
        order.setCourseCover(coursInfoOrder.getCover());//课程封面
        order.setTeacherName(coursInfoOrder.getTeacherName());//讲师名称
        order.setTotalFee(coursInfoOrder.getPrice());//课程销售价格
        order.setMemberId(memberId);//会员id
        order.setMobile(userInfoOrder.getMobile());//手机号
        order.setNickname(userInfoOrder.getNickname());//昵称
        order.setStatus(0);//订单状态（0：未支付 1：已支付）
        order.setPayType(1);//支付类型（1：微信 2：支付宝）

        System.out.println("创建一个实体类对象order，往里面放值set->完成");

        baseMapper.insert(order);
        System.out.println("往数据库放值baseMapper.insert->完成");

        String orderNo = order.getOrderNo();
        System.out.println("创建的订单号为getOrderNo："+orderNo);

        //返回订单号
        return order.getOrderNo();
    }
}
